
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import mpi.*;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Valdigleis
 */
public class HelloWorld {    
    public static void main(String args[]) throws Exception { 
        MPI.Init(args); 
        int me = MPI.COMM_WORLD.Rank(); 
        int size = MPI.COMM_WORLD.Size();
        if(me == 0){
             File file = new  File("RandVetor.txt");
             SaveVectors savevectors = new SaveVectors();
             int vetor[] = savevectors.getVector(file);
             int a, b, c, d , e, f, g, h, i, j;
             a=0; b=0; c=0; d=0; e=0; f=0; g=0; h=0; i=0; j=0;
             int vetorA[] = new int[vetor.length];
             int vetorB[] = new int[vetor.length];
             int vetorC[] = new int[vetor.length];
             int vetorD[] = new int[vetor.length];
             int vetorE[] = new int[vetor.length];
             int vetorF[] = new int[vetor.length];
             int vetorG[] = new int[vetor.length];
             int vetorH[] = new int[vetor.length];
             int vetorI[] = new int[vetor.length];
             int vetorJ[] = new int[vetor.length];
                         
             for(int x=0; x < vetor.length; x++){
                 if(vetor[x] < 0){
                     vetorA[a] = vetor[x];
                     a++;
                 }else{
                     if(vetor[x] < 20){
                        vetorB[b] = vetor[x];
                        b++;
                     }else{
                         if(vetor[x] < 40){
                            vetorC[c] = vetor[x];
                            c++;
                         }else{
                             if(vetor[x] < 60){
                                vetorD[d] = vetor[x];
                                d++;
                             }else{
                                 if(vetor[x] < 80){
                                    vetorE[e] = vetor[x];
                                    e++;
                                 }else{
                                     if(vetor[x] < 100){
                                        vetorF[f] = vetor[x];
                                        f++;
                                     }else{
                                         if(vetor[x] < 120){
                                            vetorG[g] = vetor[x];
                                            g++;
                                         }else{
                                             if(vetor[x] < 140){
                                                vetorH[h] = vetor[x];
                                                h++;
                                             }else{
                                                 if(vetor[x] < 180){
                                                    vetorI[i] = vetor[x];
                                                    i++;
                                                 }else{
                                                     vetorJ[i] = vetor[x];
                                                     j++;
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
             vetorA[a] = -999999;
             MPI.COMM_WORLD.Send(vetorA, 0, vetor.length, MPI.INT, 1, 99);
        }else{
            int vetor[] = new int[10000];
            MPI.COMM_WORLD.Recv(vetor, 0, vetor.length, MPI.INT, 0, 99);
            ArrayList<Integer> base = new ArrayList<Integer>();
            int i = 0;
            while(vetor[i] != -999999){
                base.add(vetor[i]);
                i++;
            }
            Collections.sort(base);
            for(i = 0; i < base.size(); i++){
                System.out.println(base.get(i));
            }
        }
        System.out.println("Hello i am "+me+" of total " + size);  
        MPI.Finalize(); 
    }
}